using System;
using System.Collections;
using Server;
using System.Collections.Generic;
using Server.Targeting; 
using Server.Items; 
using Server.Network;
using Server.ContextMenus;

namespace Server.Mobiles
{
	public class RepairingBlacksmith : BaseVendor
	{
		private List<SBInfo> m_SBInfos = new List<SBInfo>();
		protected override List<SBInfo> SBInfos{ get { return m_SBInfos; } }

		public override NpcGuild NpcGuild{ get{ return NpcGuild.BlacksmithsGuild; } }

		[Constructable]
		public RepairingBlacksmith() : base( "the smithing repairer" )
		{
			SetSkill( SkillName.ArmsLore, 36.0, 68.0 );
			SetSkill( SkillName.Blacksmith, 65.0, 88.0 );
			SetSkill( SkillName.Fencing, 60.0, 83.0 );
			SetSkill( SkillName.Macing, 61.0, 93.0 );
			SetSkill( SkillName.Swords, 60.0, 83.0 );
			SetSkill( SkillName.Tactics, 60.0, 83.0 );
			SetSkill( SkillName.Parry, 61.0, 93.0 );
		}

		public override void InitSBInfo()
		{
			m_SBInfos.Add( new SBBlacksmith() );
		}

		public override VendorShoeType ShoeType
		{
			get{ return Utility.RandomBool() ? VendorShoeType.Boots : VendorShoeType.ThighBoots; }
		}

		public override void InitOutfit()
		{
			base.InitOutfit();

			Item item = ( Utility.RandomBool() ? null : new Server.Items.RingmailChest() );

			if ( item != null && !EquipItem( item ) )
			{
				item.Delete();
				item = null;
			}

			if ( item == null )
				AddItem( new Server.Items.FullApron() );

			AddItem( new Server.Items.Bascinet() );
			AddItem( new Server.Items.SmithHammer() );
		}

		private class FixEntry : ContextMenuEntry
		{
			private RepairingBlacksmith m_Blacksmith;
			private Mobile m_From;

			public FixEntry( RepairingBlacksmith Blacksmith, Mobile from ) : base( 6120, 12 )
			{
				m_Blacksmith = Blacksmith;
				m_From = from;
			}

			public override void OnClick()
			{
				m_Blacksmith.BeginRepair( m_From );
			}
		}

		public override void AddCustomContextEntries( Mobile from, List<ContextMenuEntry> list )
		{
			if ( from.Alive )
			{
				list.Add( new FixEntry( this, from ) );
			}

			base.AddCustomContextEntries( from, list );
		}

		public override bool HandlesOnSpeech( Mobile from )
		{
			if( from.InRange( this.Location, 8 ) )
				return true;

			return base.HandlesOnSpeech( from );
		}

        public override void OnSpeech(SpeechEventArgs e)
        {
            if ((e.Speech.ToLower() == "repair"))
            {
                BeginRepair (e.Mobile);
            }

            else
            {
                base.OnSpeech(e);
            }
        }
        public void BeginRepair(Mobile from)
        {
            if (Deleted || !from.CheckAlive())
                return;

            SayTo(from, "You want to hire me to repair what?");

            from.Target = new RepairTarget(this);
        }

        private class RepairTarget : Target
        {
            private RepairingBlacksmith m_Blacksmith;

            public RepairTarget(RepairingBlacksmith blacksmith)
                : base(12, false, TargetFlags.None)
            {
                m_Blacksmith = blacksmith;
            }

            protected override void OnTarget(Mobile from, object targeted)
            {
                if ( ( targeted is BaseWeapon && from.Backpack != null) && !( targeted is BaseRanged ) )
                {
                    BaseWeapon bw = targeted as BaseWeapon;
                    Container pack = from.Backpack;
                    int toConsume = 0;

                    if ( bw.HitPoints < bw.MaxHitPoints )
                    {
                        toConsume = (bw.MaxHitPoints - bw.HitPoints) * 2; // 2 gp per hitpoint - change 2 to whatever you want as a multiplier
                        bw.MaxHitPoints -= 1;
                        bw.HitPoints = bw.MaxHitPoints;
                    }
					else
                    {
						m_Blacksmith.SayTo(from, "That does not need to be repaired.");
                    }

                    if (toConsume == 0)
                        return;

                    if (pack.ConsumeTotal(typeof(Gold), toConsume))
                    {
                        m_Blacksmith.SayTo(from, "Here is your weapon.");
                        from.SendMessage(String.Format("You pay {0} gold.", toConsume));
                        Effects.PlaySound(from.Location, from.Map, 0x2A);
                    }
                    else
                    {
                        m_Blacksmith.SayTo(from, "It would cost you {0} gold to have that repaired.", toConsume);
                        from.SendMessage("You do not have enough gold.");
                    }
                }
                if (targeted is BaseArmor && from.Backpack != null)
                {
                    BaseArmor ba = targeted as BaseArmor;
                    Container pack = from.Backpack;
                    int toConsume = 0;

                    if (ba.HitPoints < ba.MaxHitPoints && ba.Resource != CraftResource.RegularLeather && ba.Resource != CraftResource.SpinedLeather && ba.Resource != CraftResource.HornedLeather && ba.Resource != CraftResource.BarbedLeather )
                    {
                        toConsume = (ba.MaxHitPoints - ba.HitPoints) * 2; // 2 gp per hitpoint - change 2 to whatever you want as a multiplier
                    }
                    else if (ba.HitPoints >= ba.MaxHitPoints && ba.Resource != CraftResource.RegularLeather && ba.Resource != CraftResource.SpinedLeather && ba.Resource != CraftResource.HornedLeather && ba.Resource != CraftResource.BarbedLeather )
                    {
						m_Blacksmith.SayTo(from, "That does not need to be repaired.");
                    }
					else
					{
						m_Blacksmith.SayTo(from, "I cannot repair that.");
					}

                    if (toConsume == 0)
                        return;

                    if (pack.ConsumeTotal(typeof(Gold), toConsume))
                    {
                        m_Blacksmith.SayTo(from, "Here is your armor.");
                        from.SendMessage(String.Format("You pay {0} gold.", toConsume));
                        Effects.PlaySound(from.Location, from.Map, 0x2A);
                        ba.MaxHitPoints -=1;
                        ba.HitPoints = ba.MaxHitPoints;
                    }
                    else
                    {
                        m_Blacksmith.SayTo(from, "It would cost you {0} gold to have that repaired.", toConsume);
                        from.SendMessage("You do not have enough gold.");
                    }
                }
            }
        }

		public RepairingBlacksmith( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 ); // version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
		}
	}
}